#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
# -------------------------------------------------------------------------- #

###############################################################################
# This script is used to import a file into the marketplace. The source file
# is an opaque representation of an OpenNebula object, like a image file or a
# tar.gz with several vm template or flow disk images
###############################################################################

ONE_LOCATION = ENV["ONE_LOCATION"]

if !ONE_LOCATION
    RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
else
    RUBY_LIB_LOCATION = ONE_LOCATION + "/lib/ruby"
end

UTILS_PATH = File.join(File.dirname(__FILE__), '../../datastore')

$: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/cli"
$: << File.dirname(__FILE__)

require 'base64'
require 'rexml/document'
require 'getoptlong'
require 'open3'
require 'pp'

require 'S3'

def xpath(xml, xpath)
    xml.elements[xpath].text.to_s rescue nil
end

xml = REXML::Document.new(Base64::decode64(ARGV[0])).root

import_source = xpath(xml, 'IMPORT_SOURCE')
format        = xpath(xml, 'FORMAT')
dispose       = xpath(xml, 'DISPOSE')
size          = xpath(xml, 'SIZE')
md5           = xpath(xml, 'MD5')

id = xpath(xml, 'MARKETPLACEAPP/ID')

# required
access_key_id     = xpath(xml, 'MARKETPLACE/TEMPLATE/ACCESS_KEY_ID')
secret_access_key = xpath(xml, 'MARKETPLACE/TEMPLATE/SECRET_ACCESS_KEY')
bucket            = xpath(xml, 'MARKETPLACE/TEMPLATE/BUCKET')
region            = xpath(xml, 'MARKETPLACE/TEMPLATE/REGION')

# optional
signature_version = xpath(xml, 'MARKETPLACE/TEMPLATE/SIGNATURE_VERSION')
endpoint          = xpath(xml, 'MARKETPLACE/TEMPLATE/ENDPOINT')
force_path_style  = xpath(xml, 'MARKETPLACE/TEMPLATE/FORCE_PATH_STYLE')
read_length       = xpath(xml, 'MARKETPLACE/TEMPLATE/READx_LENGTH')

name   = "marketapp-#{id}"
source = "s3://#{bucket}/#{name}"

s3_config = {
    :region            => region,
    :access_key_id     => access_key_id,
    :secret_access_key => secret_access_key
}

s3_config[:signature_version] = signature_version if !signature_version.to_s.empty?
s3_config[:endpoint]          = endpoint if !endpoint.to_s.empty?
s3_config[:force_path_style]  = true if force_path_style.to_s.downcase == "yes"

DEFAULT_READ_LENGTH = 32*1024*1024 # Read in chunks of 32MB

if read_length.to_s.empty?
    read_length = DEFAULT_READ_LENGTH
else
    read_length = 1024*1024*read_length.to_i
end

s3 = S3.new(s3_config)

s3.name   = name
s3.bucket = bucket

if s3.exists?
    STDERR.puts "Object '#{name}' already exists."
    exit 1
end

cmd = "#{UTILS_PATH}/downloader.sh #{import_source} -"

Open3.popen3(cmd) do |_, o, _, _|
    body = o.read(read_length)

    if o.eof?
        s3.put_object(body)
    else
        s3.create_multipart_upload

        begin
            s3.upload_part(body)

            until o.eof?
                body = o.read(read_length)
                s3.upload_part(body)
            end

            s3.complete_multipart_upload
        rescue Exception => e
            STDERR.puts(e.message)
            STDERR.puts(e.backtrace)

            resp = s3.abort_multipart_upload
            STDERR.puts(resp.inspect)

            exit 1
        end
    end
end

# Result

marketplaceapp = {
    :source => source,
    :md5    => md5,
    :size   => size,
    :format => format
}

marketplaceapp.each do |k,v|
    puts "#{k.upcase}=\"#{v}\""
end

exit 0
